﻿Imports System.Data.SqlClient
Public Class searchproductlist
    Inherits System.Web.UI.Page
    Dim objrd As SqlDataReader = Nothing
    Dim objrdCom As SqlDataReader = Nothing
    Private i As Integer
    Private pages As Integer

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim name, comName, country, countryCode, strErr, strProduct, comId As String
        comName = ""
        strErr = ""
        strProduct = ""
        Dim objdt = New GetData
        Dim objdtCom = New GetData
        Dim ps = New PreparedStatement("")
        Dim psc = New PreparedStatement("")

        Dim sum, sumdesc As Integer
        Dim conn As SqlConnection = objdt.getConn()
        i = Request("page")
        If i = Nothing Or i = 0 Then
            i = 1
        End If
        If (Request("name") <> Nothing) Then
            name = Request("name")
        Else
            name = ""
        End If
        If (Request("country") <> Nothing) Then
            country = Request("country")
        Else
            country = ""
        End If
        ps.strSql = "select count(*) from product_info pi left join merchant_info mi on pi.company_id = mi.company_id" & _
                     " where pi.Product_name like '%" & name & "%' and pi.lang_flag='en' and mi.country like '%" & country & "%'"
        ps.parameters.Clear()
        objrd = objdt.dr(ps, strErr, conn, objrd)
        objrd.Read()
        sum = objrd.Item(0)
        pages = (sum \ 10) + 1
        If i >= pages Then
            i = pages
        ElseIf i <= 0 Then
            i = 1
        End If
        If (i = pages) Then
            sumdesc = sum
        Else
            sumdesc = i * 10
        End If
        totalPage.InnerText = i.ToString & "/" & pages.ToString()
        totalPage1.InnerText = i.ToString & "/" & pages.ToString()
        resultNum.InnerText = sum.ToString & "Results"

        ps.strSql = "select top(" & i & "*10) * from product_info pi left join merchant_info mi on pi.company_id = mi.company_id" & _
                     " where pi.Product_name like '%" & name & "%' and pi.lang_flag='en' and mi.country like '%" & country & "%'" & _
                    " and pi.product_id not in (" & _
                    "select top (" & i - 1 & "*10) product_id from product_info pi left join merchant_info mi on pi.company_id = mi.company_id" & _
                     " where pi.Product_name like '%" & name & "%' and pi.lang_flag='en' and mi.country like '%" & country & "%'" & _
                    " order by pi.product_name" & _
                    ") and pi.product_id not in (select top(" & sum - sumdesc & ") product_id from product_info pi left join merchant_info mi on pi.company_id = mi.company_id" & _
                     " where pi.Product_name like '%" & name & "%' and pi.lang_flag='en' and mi.country like '%" & country & "%' " & _
                    "order by pi.product_name desc) order by pi.product_name"
        objrd = objdt.dr(ps, strErr, conn, objrd)
        If objrd.HasRows Then
            Dim con As SqlConnection = objdtCom.getConn()

            While (objrd.Read())
                comId = objrd.Item("company_id")
                psc.strSql = "select * from merchant_info where company_id = @comId order by company_name"
                psc.addParameter("comId", comId)
                objrdCom = objdtCom.dr(psc, strErr, con, objrdCom)
                If objrdCom.HasRows Then
                    objrdCom.Read()
                    comName = objrdCom.Item("company_name")
                    country = objrdCom.Item("country")
                    If (country.ToLower.Equals("china")) Then
                        countryCode = "../assets/cn.gif"
                    ElseIf (country.ToLower.Equals("usa")) Then
                        countryCode = "../assets/us.gif"
                    End If
                End If
                strProduct &= "<div class=""itemBox"">" & _
                            "<div class=""box2"">" & _
                                "<div class=""images"">" & _
                                    "<a target=""_blank"" dot=""area=ml2&type;=pimg&value;="" value=""#"">" & _
                                        "<img  border=""0"" isoem=""true"" alt=""" & objrd.Item("Product_name") & """ src=""../assets/product_img_123456789.jpg""/>" & _
                                    "</a>" & _
                                "</div>" & _
                            "</div>" & _
                            "<div class=""box5 products"">" & _
                                "<div style=""margin-bottom: 5px;"">" & _
                                    "Supplier:" & _
                                    "<a class=""company"" target=""_blank"" dot=""area=ml2&type;=pttl&value;="" href=""company.aspx?cId=" & comId & """>" & comName & "</a>" & _
                                "</div>" & _
                                "<div class=""location"">" & _
                                    "<img style=""border: 0pt none; vertical-align: top;"" alt=""country flag"" src=""" & countryCode & """/>" & _
                                    country & _
                                "</div>" & _
                            "</div>" & _
                            "<div class=""box3"">" & _
                                "<div class=""productname"">" & _
                                    "<a  target=""_blank"" title="""" dot=""area=ml2&type;=pttl&value;="" href=""productdetail.aspx?productId=" & objrd.Item("product_id") & """>" & _
                                    objrd.Item("Product_name") & _
                                    "</a>" & _
                                "</div>" & _
                                "<div class=""description"">" & _
                                    objrd.Item("description") & _
                                    "<br>" & _
                                "</div>" & _
                            "</div>" & _
                        "</div>"
            End While
            objdtCom.closeConn(con)

        End If
        objdt.closeConn(conn)
        Me.country.Value = Request("country")
        Me.name.Value = Request("name")
        prePage.HRef = "searchproductlist.aspx?country=" & Request("country") & "&name=" & name & "&page=" & i - 1
        nextPage.HRef = "searchproductlist.aspx?country=" & Request("country") & "&name=" & name & "&page=" & i + 1
        productList.InnerHtml = strProduct
    End Sub

    Protected Sub go_Click(ByVal sender As Object, ByVal e As EventArgs) Handles go.Click
        Response.Redirect("searchproductlist.aspx?country=" & Me.country.Value & "&name=" & Me.name.Value & "&page=" & pageNum.Text)
    End Sub
End Class